/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import mx.edu.itschapala.sistemas.septimo.control.ClientelnLocal;
import mx.edu.itschapala.sistemas.septimo.control.ProductoLnLocal;
import mx.edu.itschapala.sistemas.septimo.control.VentaLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.modelo.Venta;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoBusqueda;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVista;

/**
 *
 * @author Usuario
 */
public class VentaLista extends javax.swing.JFrame {
String renglon[]= new String[6];
    private JFrame venta;//variable para administar ventanas
    private VentaLnLocal ventaControl ;
     private ProductoLnLocal productoControl;
    private ClientelnLocal ClienteControl;
    private List<Venta> lista;
     private List<Producto> listaP;
      private List<Venta> lista1;
     private List<Producto> listaP1;
    private DefaultTableModel modelo;
//    private String [] l= new String[100] ;
//     private String [] g= new String[100] ;
//   int y =0;
//   int c = 0;
    
    public VentaLista(VentaLnLocal ventaControl,ProductoLnLocal productoControl,ClientelnLocal ClienteControl) {
        this.ClienteControl=ClienteControl;
        this.productoControl=productoControl;
        this.ventaControl=ventaControl;
         listaP= productoControl.getTodos();
        lista=ventaControl.getTodos();

        initComponents();

              
           
//        for(int x =0; x<=lista.size()-1 ;x++){
//             for(int y =0; y<=listaP.size()-1 ;y++){
//                 if(lista.get(x).getProducto()==listaP.get(y).getNombre()){
//                   l[x]=listaP.get(x).getPrecio()+"";  
//                    
//             }
//           
//             }
//       }
           
        
        this.setResizable(false);
     String nombres[]={"fecha","id","cliente","producto","hora","precio"};
        modelo = new DefaultTableModel(nombres,0){
          
         public boolean isCellEditable(int row, int column){
                return false;
            }
        };
       // DefaultTableModel modelo = new DefaultTableModel(nombres,0);
       
        jTable1.setModel(modelo);
       
     
        for(Venta v : lista){
            
            String renglon[]= new String[6];
          renglon[0]=v.getFecha();
            renglon[1]=v.getId()+"";
            renglon[2]=v.getCliente();
            renglon[3]=v.getProducto().getNombre();
            renglon[4]=v.getHora();
           renglon[5]=v.getProducto().getPrecio()+"";
          
            modelo.addRow(renglon);
    
        }
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextField1 = new javax.swing.JTextField();
        jRadioButton2 = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jRadioButton2.setText("inica");

        jLabel1.setText("Ventana");

        jRadioButton1.setText("contiene");

        jButton1.setText("Registrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("actualizar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(jLabel1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(15, 15, 15))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(31, 31, 31)
                        .addComponent(jButton1)
                        .addGap(115, 115, 115))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if(evt.getClickCount()==2){
            Venta  v = lista.get(jTable1.getSelectedRow());

            if(venta==null){
                venta = new DialogoVenta(v,ventaControl);
            }else{
                venta.dispose();
                venta = new DialogoVenta(v,ventaControl);
            }
            venta.setVisible(true);
            this.dispose();
        }

    }//GEN-LAST:event_jTable1MouseClicked

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
        List <Venta> tmp = new ArrayList<>();
        if(jRadioButton1.isSelected()){
            
           tmp=ventaControl.getPorCliente(jTextField1.getText()+evt.getKeyChar(), TipoBusqueda.CONTIENE);
        }else{
            tmp=ventaControl.getPorCliente(jTextField1.getText()+evt.getKeyChar(), TipoBusqueda.INICIA);
        }
        String nombres[]={"fecha","id","cliente","producto","hora","precio"};
        DefaultTableModel modelo = new DefaultTableModel(nombres,0){
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };
        jTable1.setModel(modelo);
        for(Venta v : tmp){

            String renglon[]= new String[6];
            renglon[0]=v.getFecha();
            renglon[1]=v.getId()+"";
            renglon[2]=v.getCliente();
            renglon[3]=v.getProducto().getNombre();
            renglon[4]=v.getHora();
             renglon[5]=v.getProducto().getPrecio()+"";
            modelo.addRow(renglon);
//y++;
        }
// y=0;
        jTable1.setModel(modelo);
        lista=tmp;
    }//GEN-LAST:event_jTextField1KeyTyped

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        new VentaCrearEditar(null,productoControl,ClienteControl,ventaControl,TipoVista.VENTANA_REGISTRO).setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
//       listaP1= productoControl.getTodos();
//        lista=ventaControl.getTodos();
//              for(int x =0; x<=lista.size()-1 ;x++){
//             for(int y =0; y<=listaP.size()-1 ;y++){
//                 if(lista.get(x).getProducto()==listaP1.get(y).getNombre()){
//                   l[x]=listaP.get(x).getPrecio()+"";  
//                     
//             }
//           
//             }
//       }
                 
    
      
        
        
     
         String nombres[]={"fecha","id","cliente","producto","hora","precio"};
        modelo = new DefaultTableModel(nombres,0){
          
         public boolean isCellEditable(int row, int column){
                return false;
            }
        };
       // DefaultTableModel modelo = new DefaultTableModel(nombres,0);
       
        jTable1.setModel(modelo);
       
     
        for(Venta v : lista){
            
            String renglon[]= new String[6];
          renglon[0]=v.getFecha();
            renglon[1]=v.getId()+"";
            renglon[2]=v.getCliente();
            
            renglon[3]= v.getProducto().getNombre()+"";
           
            renglon[4]=v.getHora();
           renglon[5]=v.getProducto().getPrecio()+"";
      
            modelo.addRow(renglon);
//    y++;
        }
//        y=0;
//        System.out.println(y);
        
//     
//        String nombres[]={"fecha","id","cliente","producto","hora","precio"};
//        modelo = new DefaultTableModel(nombres,0){
//
//            public boolean isCellEditable(int row, int column){
//                return false;
//            }
//        };
//  
//
//        jTable1.setModel(modelo);
//        lista=ventaControl.getTodos();
//        for(Venta v : lista){
//            String renglon[]= new String[6];
//         renglon[0]=v.getFecha();
//            renglon[1]=v.getId()+"";
//            renglon[2]=v.getCliente();
//            renglon[3]=v.getProducto();
//            renglon[4]=v.getHora();
//             renglon[5]=l[c]+"";
//             System.out.println(l[c]+"");
//            
//            modelo.addRow(renglon);
//c++;
//        }
//         y=0;
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
